package study.week05.config;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import study.week05.entity.Klass;
import study.week05.entity.School;
import study.week05.entity.Student;

public class CustomBeanDefinitionParser implements BeanDefinitionParser {
    private final Class<?> beanClass;

    public CustomBeanDefinitionParser(Class<?> beanClass) {
        this.beanClass = beanClass;
    }
    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(beanClass);
        if(beanClass.equals(Student.class)){
            genericBeanDefinition.setLazyInit(false);
            genericBeanDefinition.getPropertyValues().add("studentName", element.getAttribute("studentName"));
            genericBeanDefinition.getPropertyValues().add("age", element.getAttribute("age"));
        }
        if(beanClass.equals(Klass.class)){
            genericBeanDefinition.setLazyInit(false);
            genericBeanDefinition.getPropertyValues().add("className", element.getAttribute("className"));
            genericBeanDefinition.getPropertyValues().add("grade", element.getAttribute("grade"));
        }
        if(beanClass.equals(School.class)){
            genericBeanDefinition.setLazyInit(false);
            genericBeanDefinition.getPropertyValues().add("schoolName", element.getAttribute("schoolName"));
            genericBeanDefinition.getPropertyValues().add("address", element.getAttribute("address"));
        }
        parserContext.getRegistry().registerBeanDefinition(beanClass.getName(),genericBeanDefinition);
        return null;
    }
}